<?php
try {
	if($_SERVER['REQUEST_METHOD'] == 'POST'){
		if(!isset($_POST['email'], $_POST['password'])) {
			echo 'need parameter email, password';
			Helper::refresh();
		}
		Authenticate::login($_POST['email'], $_POST['password']);
		if(Authenticate::getErrorNo() === 0){
			echo 'Login success';
			if(isset($_GET['redir'])){
				header("Location: {$_GET['redir']}");
			}
			else{
				header("Location: old.php?page=user/profile");
			}
		}
		else if(Authenticate::getErrorNo() === Authenticate::EMAIL_NOT_FOUND){
			echo 'ไม่พบ email นี้';
			Helper::refresh();
		}
		else if(Authenticate::getErrorNo() === Authenticate::PASSWORD_INVALID){
			echo 'password ผิด';
			Helper::refresh();
		}
		else if(Authenticate::getErrorNo() === Authenticate::ACCOUNT_NOT_VALIDATE){
			echo 'Account นี้ ยังไม่ได้ทำการ Activate';
			Helper::refresh();
		}
	}
	else if($_SERVER['REQUEST_METHOD'] == 'GET'){
		View::display('login');
	}
}
catch (Exception $ex){
	echo $ex->getMessage();
	throw $ex;
}